package com.boang.controller;

import java.io.*;
import java.util.Random;
import java.util.UUID;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.OSSObjectSummary;
import com.boang.entity.resp.Result;
import com.boang.exception.StorageException;
import com.boang.factory.StorageServiceFactory;
import com.boang.service.CloudStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/files")
public class OssController {

    @Autowired
    private StorageServiceFactory factory;

    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
        CloudStorageService service = null;
        try {
            service = factory.getActiveService();
            return Result.ok(service.uploadFile(file));
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
}